import axios from 'axios'
import servers from '../servers'
import store from '../../store/index'


let baseRequest = function (path,method,requestParams,requestBody,handler) {
  axios({
    // url: servers.server + path,
    url: path,
    method: method,
    params: requestParams,
    data: requestBody,
    headers: {'Authorization': store.getters['auth/getLoginSessionState'].token},
    withCredentials: false
  }).then(function (res) {
    if (res.status === 200) {
      if (handler && handler.success){
        handler.success(res);
      }
    }else {
      console.log(servers.server + path + " 请求失败：" + res);
    }
  }).catch(function (e) {
    console.error(e);
    if (handler && handler.error) {
      handler.error(e);
    } else {
      console.error(e);
    }
  })
};

let get = function (path, requestParams, handler) {
  baseRequest(path, 'get', requestParams, null, handler)
};

let post = function (path, requestParams, requestBody, handler) {
  baseRequest(path, 'post', requestParams, requestBody, handler)
};

let postParams = function (path, requestParams, handler) {
  baseRequest(path, 'post', requestParams, null, handler)
};

let postBody = function (path, requestBody, handler) {
  baseRequest(path, 'post', null, requestBody, handler)
};

export default {
  get: get,
  post: post,
  postParams: postParams,
  postBody: postBody
}
